package com.jswlw.service.helper;

import com.alibaba.fastjson.JSON;
import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.controller.devapi.req.SyncDayMessage;
import com.jswlw.entity.biz.*;
import com.jswlw.service.biz.*;
import com.jswlw.service.helper.message.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Slf4j
@Component
public class DevApiHelper {

    @Autowired
    HttpHelper httpHelper;
    @Autowired
    BindRecordService bindRecordService;
    @Autowired
    DevSetService devSetService;
    @Autowired
    DevInfoService devInfoService;
    @Autowired
    DevFilterService devFilterService;
    @Autowired
    FilterInfoService filterInfoService;


    /**
     * 绑定套餐
     *
     * @param message
     */
    @Value("${dev.api.url.bindPricing}")
    String bindPricing;

    public boolean bindPricing(
            String icCid,
            int gzms,
            int dyijlxzdz,
            int derjlxzdz,
            int dsanjlxzdz,
            int dsijlxzdz,
            int dwujlxzdz) {
        log.info("绑定套餐:" + icCid);
        BindPricingMessage message = new BindPricingMessage();
        message.setCmd(CmdType.BindPricing.getCmd());
        message.setImei(icCid);
        message.setGzms(gzms);
        message.setDyijlxzdz(dyijlxzdz);
        message.setDerjlxzdz(derjlxzdz);
        message.setDsanjlxzdz(dsanjlxzdz);
        message.setDsijlxzdz(dsijlxzdz);
        message.setDwujlxzdz(dwujlxzdz);

        BindRecord bindRecord = new BindRecord();
        bindRecord.setImei(message.getImei());
        bindRecord.setContent(JSON.toJSONString(message));
        bindRecord.setCreateTime(new Date());
        bindRecordService.add(bindRecord);
        return httpHelper.post(bindPricing, JSON.toJSONString(message));
    }

    /**
     * 打开屏幕
     *
     * @param message
     */
    @Value("${dev.api.url.screenOpen}")
    String screenOpen;

    public void screenOpen(String icCid) {
        log.info("打开屏幕:" + icCid);
        ScreenOpenMessage message = new ScreenOpenMessage();
        message.setImei(icCid);
        message.setCmd(CmdType.ScreenOpen.getCmd());
        httpHelper.post(screenOpen, JSON.toJSONString(message));
    }

    /**
     * 关闭屏幕
     *
     * @param message
     */
    @Value("${dev.api.url.screenClose}")
    String screenClose;

    public void screenClose(String icCid) {
        log.info("关闭屏幕:" + icCid);
        ScreenCloseMessage message = new ScreenCloseMessage();
        message.setImei(icCid);
        message.setCmd(CmdType.ScreenClose.getCmd());
        httpHelper.post(screenClose, JSON.toJSONString(message));
    }

    /**
     * 开机
     *
     * @param message
     */
    @Value("${dev.api.url.startUp}")
    String startUp;

    public void startUp(String icCid, byte state) {
        log.info("开机:" + icCid);
        StartUpMessage message = new StartUpMessage();
        message.setImei(icCid);
        message.setState(state);
        message.setCmd(CmdType.Startup.getCmd());
        httpHelper.post(startUp, JSON.toJSONString(message));
    }

    /**
     * 关机
     *
     * @param message
     */
    @Value("${dev.api.url.shutDown}")
    String shutDown;

    public void shutDown(String icCid, byte state) {
        log.info("关机:" + icCid);
        ShutdownMessage message = new ShutdownMessage();
        message.setImei(icCid);
        message.setState(state);
        message.setCmd(CmdType.Shutdown.getCmd());
        httpHelper.post(shutDown, JSON.toJSONString(message));
    }

    /**
     * 强冲
     *
     * @param message
     */
    @Value("${dev.api.url.forceWash}")
    String forceWash;

    public void forceWash(String icCid, byte state) {
        log.info("强冲:" + icCid);
        ForceWashMessage message = new ForceWashMessage();
        message.setImei(icCid);
        message.setState(state);
        message.setCmd(CmdType.ForceWash.getCmd());
        httpHelper.post(forceWash, JSON.toJSONString(message));
    }

    /**
     * 冲正值
     *
     * @param message
     */
    @Value("${dev.api.url.addMoney}")
    String addMoney;

    public void addMoney(String icCid, int charge) {
        log.info("冲正值:" + icCid);
        AddMoneyMessage message = new AddMoneyMessage();
        message.setImei(icCid);
        message.setCharge(charge);
        message.setCmd(CmdType.AddMoney.getCmd());
        httpHelper.post(addMoney, JSON.toJSONString(message));
    }

    /**
     * 冲负值
     *
     * @param message
     */
    @Value("${dev.api.url.subMoney}")
    String subMoney;

    public void subMoney(String icCid, int charge) {
        log.info("冲负值:" + icCid);
        SubMoneyMessage message = new SubMoneyMessage();
        message.setImei(icCid);
        message.setCharge(charge);
        message.setCmd(CmdType.SubMoney.getCmd());
        httpHelper.post(subMoney, JSON.toJSONString(message));
    }

    /**
     * 恢复出厂
     *
     * @param message
     */
    @Value("${dev.api.url.wipe}")
    String wipe;

    public void wipe(String icCid) {
        log.info("恢复出厂:" + icCid);
        WipeDataMessage message = new WipeDataMessage();
        message.setImei(icCid);
        message.setCmd(CmdType.WipeData.getCmd());
        httpHelper.post(wipe, JSON.toJSONString(message));
    }

    /**
     * 更新滤芯
     *
     * @param message
     */
    @Value("${dev.api.url.filterUpdate}")
    String filterUpdate;

    public void filterUpdate(String icCid, byte code1, byte code2, byte code3) {
        log.info("滤芯初始:" + icCid);
        FilterUpdateMessage message = new FilterUpdateMessage();
        message.setImei(icCid);
        message.setCode1(code1);
        message.setCode2(code2);
        message.setCode3(code3);
        message.setCmd(CmdType.UpdateFilter.getCmd());
        httpHelper.post(filterUpdate, JSON.toJSONString(message));
    }

    /**
     * 查询设备信息
     *
     * @param message
     * @return
     */
    @Value("${dev.api.url.devInfo}")
    String devInfo;

    public boolean devInfo(String icCid, byte state) {
        QueryDeviceRunInfoMessage message = new QueryDeviceRunInfoMessage();
        message.setImei(icCid);
        message.setState(state);
        return httpHelper.post(devInfo, JSON.toJSONString(message));
    }

    /**
     * 修改心跳周期
     *
     * @param message
     * @return
     */
    @Value("${dev.api.url.updateHeart}")
    String updateHeart;

    public boolean updateHeart(String icCid, byte interval) {
        UpdateHeartbeatIntervalMessage message = new UpdateHeartbeatIntervalMessage();
        message.setImei(icCid);
        message.setInterval(interval);
        message.setCmd(CmdType.UpdateHeartbeatInterval.getCmd());
        return httpHelper.post(updateHeart, JSON.toJSONString(message));
    }

    /**
     * 模式切换
     *
     * @param message
     */
    @Value("${dev.api.url.switchBillMode}")
    String switchBillMode;

    public void switchBillMode(String imei, Integer ll2sc, byte model) {
        log.info("模式切换:" + imei);
        SwitchBillModelMessage message = new SwitchBillModelMessage();
        message.setImei(imei);
        message.setModel(model);
        message.setScale(ll2sc.shortValue());
        message.setCmd(CmdType.SwitchBillModel.getCmd());
        httpHelper.post(switchBillMode, JSON.toJSONString(message));
        //模式切换，更新滤芯的值
//        DevInfo devInfo = devInfoService.find4Imei(imei);
//        List<DevFilter> devFilterList = devFilterService.find4DevId(devInfo.getId());
//        if (!CollectionUtils.isEmpty(devFilterList)) {
//            devFilterList.forEach(devFilter -> {
//                if (model == 1) {
//                    int remain = (devFilter.getMaxValue() - devFilter.getUsed()) * 10 / 137;
//                    int max = devFilter.getMaxValue() * 10 / 137;
//                    filterUpdate(imei, devFilter.getSort().byteValue(), (byte) remain, (byte) max);
//                } else if (model == 0) {
//                    int remain = (devFilter.getMaxValue() - devFilter.getUsed()) * 137 / 10;
//                    int max = devFilter.getMaxValue() * 137 / 10;
//                    filterUpdate(imei, devFilter.getSort().byteValue(), (byte) remain, (byte) max);
//                }
//            });
//        }
    }

    @Value("${dev.api.url.modifyHostPort}")
    String modifyHostPort;

    public void modifyHostPort(String icCid, String host, int port) {
        ModifyHostPortMessage message = new ModifyHostPortMessage();
        message.setImei(icCid);
        message.setHost(host);
        message.setPort(port);
        httpHelper.post(modifyHostPort, JSON.toJSONString(message));
    }

    @Value("${dev.api.url.rw_wash_time}")
    String rw_wash_time;

    public Integer modifyWashTime(String icCid, short seconds) {
        ModifyWashTimeMessage message = new ModifyWashTimeMessage();
        message.setImei(icCid);
        message.setSeconds(seconds);
        message.setCmd(CmdType.ModifyWashTime.getCmd());
        httpHelper.post(rw_wash_time, JSON.toJSONString(message));
        return 0;
    }

    @Value("${dev.api.url.rw_wash_fix_time}")
    String modifyWashFixedTime;

    public Integer modifyWashFixedTime(String icCid, short minutes) {
        ReadOrModifyWashFixedTimeMessage message = new ReadOrModifyWashFixedTimeMessage();
        message.setImei(icCid);
        message.setMinutes(minutes);
        message.setCmd(CmdType.RWWashTime.getCmd());
        httpHelper.post(modifyWashFixedTime, JSON.toJSONString(message));
        return 0;
    }

    @Value("${dev.api.url.rw_service_time}")
    String modifyServerTime;

    public Integer modifyServerTime(String icCid, short minutes) {
        ReadOrModifyServiceTimeMessage message = new ReadOrModifyServiceTimeMessage();
        message.setImei(icCid);
        message.setMinutes(minutes);
        message.setCmd(CmdType.RWServiceTime.getCmd());
        httpHelper.post(modifyServerTime, JSON.toJSONString(message));
        return 0;
    }

    @Value("${dev.api.url.rw_run_time}")
    String rw_run_time;

    public Integer rw_run_time(String icCid, short minutes) {
        ReadOrModifyServiceTimeMessage message = new ReadOrModifyServiceTimeMessage();
        message.setImei(icCid);
        message.setMinutes(minutes);
        message.setCmd(CmdType.PumpWorkTime.getCmd());
        httpHelper.post(rw_run_time, JSON.toJSONString(message));
        return 0;

    }

    @Value("${dev.api.url.sync_day}")
    String sync_day;

    public boolean sync_day(String icCid, short day) {
        SyncDayMessage message = new SyncDayMessage();
        message.setImei(icCid);
        message.setDay(day);
        message.setCmd(CmdType.SyncTime.getCmd());
        return httpHelper.post(sync_day, JSON.toJSONString(message));
    }

    @Value("${dev.api.url.sync_water}")
    String sync_water;

    public void sync_water(String icCid, short day) {
        SyncDayMessage message = new SyncDayMessage();
        message.setImei(icCid);
        message.setDay(day);
        message.setCmd(CmdType.SyncWater.getCmd());
        httpHelper.post(sync_water, JSON.toJSONString(message));
    }

    @Value("${dev.api.url.rw_one_water_of_time}")
    String rw_one_water_of_time;

    public int rw_one_water_of_time(String imei, short minutes) {
        RwOneWaterOfTime message = new RwOneWaterOfTime();
        message.setImei(imei);
        message.setMinutes(minutes);
        message.setCmd(CmdType.RwOneWaterOfTime.getCmd());
        httpHelper.post(rw_one_water_of_time, JSON.toJSONString(message));
        return 0;
    }
}
